﻿using PSI.Helper;
using PSI.Services;
using PSI.Services.TMS;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace PSI.Controllers {
    public class OutPlanController : PSIController {
        public ActionResult Index() {
            var ps = new PermissionService();
            if (!ps.HasPermission(FIdConst.OUT_PLAN)) {
                return Redirect("/");
            }

            ViewBag.UserName = UserService.GetLoginUserName();

            ViewBag.ConsignationBillNumber = this.Request.Params["billNumber"];
            ViewBag.PlanNumber = this.Request.Params["planNumber"];
            return View();
        }

        [HttpPost]
        public ActionResult GetOutPlanListData() {
            var data = new OutPlanService().GetOutPlanListData(this.Request.Form);

            return Json(data);
        }

        [HttpPost]
        public ActionResult GetTodayPlanListData() {
            var data = new OutPlanReportService().GenReportCurrentMonth();

            return Json(data);
        }

        [HttpPost]
        public ActionResult MakePlan() {
            var result = new OutPlanService().MakePlan(this.Request.Form);

            return Json(result);
        }

        [HttpPost]
        public ActionResult ChangeDispatcher() {
            var result = new OutPlanService().ChangeDispatcher(this.Request.Form);

            return Json(result);
        }

        [HttpPost]
        public ActionResult GetSendTruckInfo(string id) {
            var data = new OutPlanService().GetSendTruckInfo(id);

            return Json(data);
        }

        [HttpPost]
        public ActionResult SendTruck() {
            var result = new OutPlanService().SendTruck(this.Request.Form);

            return Json(result);
        }

        [HttpPost]
        public ActionResult GetTraceInfo(string id) {
            var data = new OutPlanService().GetTraceInfo(id);

            return Json(data);
        }

        [HttpPost]
        public ActionResult Trace() {
            var result = new OutPlanService().Trace(this.Request.Form);

            return Json(result);
        }

        [HttpPost]
        public ActionResult TraceComments() {
            var result = new OutPlanService().TraceComments(this.Request.Form);

            return Json(result);
        }

        [HttpPost]
        public ActionResult GetCntNumber(string id) {
            var data = new OutPlanService().GetCntNumber(id);

            return Json(data);
        }

        [HttpPost]
        public ActionResult ReportCntNum() {
            var result = new OutPlanService().ReportCntNum(this.Request.Form);

            return Json(result);
        }

        [HttpPost]
        public ActionResult CheckCntNumber(string cntNumber1, string cntNumber2) {
            var result = new OutPlanService().CheckCntNumber(cntNumber1, cntNumber2);

            return Json(result);
        }

        public ActionResult Excel() {
            var fileName = new OutPlanService().Excel(this.Request.Params, Server.MapPath("/"));

            return File(fileName, "application/vnd.ms-excel", "出口计划.xls");
        }

        public ActionResult MakePlanIndex() {
            var ps = new PermissionService();
            if (!ps.HasPermission(FIdConst.OUT_PLAN_MAKE_PLAN)) {
                return Redirect("/");
            }

            ViewBag.UserName = UserService.GetLoginUserName();

            return View();
        }

        [HttpPost]
        public ActionResult GetMakePlanListData() {
            var data = new OutPlanService().GetMakePlanListData();

            return Json(data);
        }


        public ActionResult SendTruckIndex() {
            var ps = new PermissionService();
            if (!ps.HasPermission(FIdConst.OUT_PLAN_SEND_TRUCK)) {
                return Redirect("/");
            }

            ViewBag.UserName = UserService.GetLoginUserName();

            return View();
        }

        [HttpPost]
        public ActionResult GetSendTruckListData() {
            var data = new OutPlanService().GetSendTruckListData(this.Request.Form);

            return Json(data);
        }

        public ActionResult TraceIndex() {
            var ps = new PermissionService();
            if (!ps.HasPermission(FIdConst.OUT_PLAN_TRACE)) {
                return Redirect("/");
            }

            ViewBag.UserName = UserService.GetLoginUserName();

            return View();
        }

        [HttpPost]
        public ActionResult GetTraceListData() {
            var data = new OutPlanService().GetTraceListData();

            return Json(data);
        }

        [HttpPost]
        public ActionResult QueryTripNumber(string truckNumber, string queryKey) {
            var data = new OutPlanService().QueryTripNumber(truckNumber, queryKey);

            return Json(data);
        }

        [HttpPost]
        public ActionResult CancelPlan(string id) {
            var result = new OutPlanService().CancelPlan(id);

            return Json(result);
        }

        [HttpPost]
        public ActionResult CancelTruck(string id) {
            var result = new OutPlanService().CancelTruck(id);

            return Json(result);
        }

        [HttpPost]
        public ActionResult Finish(string id) {
            var result = new OutPlanService().Finish(id);

            return Json(result);
        }

        [HttpPost]
        public ActionResult GetTraceComments(string id) {
            var data = new OutPlanService().GetTraceComments(id);

            return Json(data);
        }

        public ActionResult EvaluateService()
        {
            ViewBag.UserName = UserService.GetLoginUserName();
            return View();
        }


        public ActionResult ExportPlanReport()
        {
            ViewBag.UserName = UserService.GetLoginUserName();
            return View();
        }

        [HttpPost]
        public ActionResult GetExReport()
        {
            var dt = Request["dt"];

            var data = new OutPlanReportService().GetJKJHHZB(dt);

            return Json(new { Data = data });
        }
    }
}
